package com.ruoyi.activieadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.activieadmin.mapper.AaActiveDetailPhotoMapper;
import com.ruoyi.activieadmin.service.IAaActiveDetailPhotoService;
import com.ruoyi.activieadmin.domain.AaActiveDetailPhoto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.activieadmin.domain.AaActiveDetail;
import com.ruoyi.activieadmin.service.IAaActiveDetailService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AaActiveDetailPhotoServiceImpl implements IAaActiveDetailPhotoService {


    @Resource
    private AaActiveDetailPhotoMapper aaActiveDetailPhotoMapper;
    
    
    @Resource
    private IAaActiveDetailService aaActiveDetailService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param aaActiveDetailPhoto 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<AaActiveDetailPhoto> getAaActiveDetailPhotoPage(PageDomain pageDomain, AaActiveDetailPhoto aaActiveDetailPhoto , AaActiveDetail aaActiveDetail) {
        if (Objects.isNull(aaActiveDetailPhoto)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<AaActiveDetailPhoto> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetailPhoto());
            //if(!Objects.isNull(aaActiveDetailPhoto.getName())){
            //	queryWrapper.like(AaActiveDetailPhoto::getName,aaActiveDetailPhoto.getName());
            //}
            

            if (!Objects.isNull(aaActiveDetailPhoto.getId())) {
                queryWrapper.eq(AaActiveDetailPhoto::getId, aaActiveDetailPhoto.getId());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getAaActiveDetailId())) {
                queryWrapper.eq(AaActiveDetailPhoto::getAaActiveDetailId, aaActiveDetailPhoto.getAaActiveDetailId());
            }

            aaActiveDetail.setBaseEntityNull();
            aaActiveDetail.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(aaActiveDetail)) {
                List<Long> idList = aaActiveDetailService.getIdListByAaActiveDetail(aaActiveDetail);
                queryWrapper.in(AaActiveDetailPhoto::getAaActiveDetailId, idList);
            }


            if (!Objects.isNull(aaActiveDetailPhoto.getPhotoUrl())) {
                queryWrapper.like(AaActiveDetailPhoto::getPhotoUrl, aaActiveDetailPhoto.getPhotoUrl());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getStatus())) {
                queryWrapper.like(AaActiveDetailPhoto::getStatus, aaActiveDetailPhoto.getStatus());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getDelFlag())) {
                queryWrapper.like(AaActiveDetailPhoto::getDelFlag, aaActiveDetailPhoto.getDelFlag());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getCreateBy())) {
                queryWrapper.like(AaActiveDetailPhoto::getCreateBy, aaActiveDetailPhoto.getCreateBy());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetailPhoto::getCreateTime, aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetailPhoto::getCreateTime, aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetailPhoto::getCreateTime, aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"), aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getUpdateBy())) {
                queryWrapper.like(AaActiveDetailPhoto::getUpdateBy, aaActiveDetailPhoto.getUpdateBy());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetailPhoto::getUpdateTime, aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetailPhoto::getUpdateTime, aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetailPhoto::getUpdateTime, aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"), aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getRemark())) {
                queryWrapper.like(AaActiveDetailPhoto::getRemark, aaActiveDetailPhoto.getRemark());
            }
        Page<AaActiveDetailPhoto> aaActiveDetailPhotoList= aaActiveDetailPhotoMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(AaActiveDetailPhoto record:aaActiveDetailPhotoList.getRecords()){
            record.setAaActiveDetail(aaActiveDetailService.getAaActiveDetailById(record.getAaActiveDetailId()));
        }

            return aaActiveDetailPhotoList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public AaActiveDetailPhoto getAaActiveDetailPhotoById(Long id) {
        if (Objects.isNull(id)) {
            return new AaActiveDetailPhoto();
        }
        try {
            AaActiveDetailPhoto aaActiveDetailPhoto = aaActiveDetailPhotoMapper.selectById(id);
            aaActiveDetailPhoto.setAaActiveDetail(aaActiveDetailService.getAaActiveDetailById(aaActiveDetailPhoto.getAaActiveDetailId()));
            if (Objects.isNull(aaActiveDetailPhoto)){
                aaActiveDetailPhoto = new AaActiveDetailPhoto();
            }
            return aaActiveDetailPhoto;

        } catch (Exception e) {
            e.printStackTrace();
            return new AaActiveDetailPhoto();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param aaActiveDetailPhoto 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByAaActiveDetailPhoto(AaActiveDetailPhoto aaActiveDetailPhoto) {
        if (Objects.isNull(aaActiveDetailPhoto)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<AaActiveDetailPhoto> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetailPhoto());
            

            if (!Objects.isNull(aaActiveDetailPhoto.getId())) {
                queryWrapper.eq(AaActiveDetailPhoto::getId, aaActiveDetailPhoto.getId());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getAaActiveDetailId())) {
                queryWrapper.eq(AaActiveDetailPhoto::getAaActiveDetailId, aaActiveDetailPhoto.getAaActiveDetailId());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getPhotoUrl())) {
                queryWrapper.like(AaActiveDetailPhoto::getPhotoUrl, aaActiveDetailPhoto.getPhotoUrl());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getStatus())) {
                queryWrapper.like(AaActiveDetailPhoto::getStatus, aaActiveDetailPhoto.getStatus());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getDelFlag())) {
                queryWrapper.like(AaActiveDetailPhoto::getDelFlag, aaActiveDetailPhoto.getDelFlag());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getCreateBy())) {
                queryWrapper.like(AaActiveDetailPhoto::getCreateBy, aaActiveDetailPhoto.getCreateBy());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetailPhoto::getCreateTime, aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetailPhoto::getCreateTime, aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetailPhoto::getCreateTime, aaActiveDetailPhoto.getParams().get("CreateTimeBeginTime"), aaActiveDetailPhoto.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getUpdateBy())) {
                queryWrapper.like(AaActiveDetailPhoto::getUpdateBy, aaActiveDetailPhoto.getUpdateBy());
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetailPhoto::getUpdateTime, aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetailPhoto::getUpdateTime, aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetailPhoto::getUpdateTime, aaActiveDetailPhoto.getParams().get("UpdateTimeBeginTime"), aaActiveDetailPhoto.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetailPhoto.getRemark())) {
                queryWrapper.like(AaActiveDetailPhoto::getRemark, aaActiveDetailPhoto.getRemark());
            }

            List<AaActiveDetailPhoto> aaActiveDetailPhotos = aaActiveDetailPhotoMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveDetailPhoto aaActiveDetailPhotoNo:aaActiveDetailPhotos){
                idList.add(aaActiveDetailPhotoNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过aaActiveDetail外键idList查询idList
     *
     * @param aaActiveDetailIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByAaActiveDetailIdList(List<Long> aaActiveDetailIdList) {
        if (Objects.isNull(aaActiveDetailIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<AaActiveDetailPhoto> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetailPhoto());
            queryWrapper.in(AaActiveDetailPhoto::getAaActiveDetailId, aaActiveDetailIdList);
            List<AaActiveDetailPhoto> aaActiveDetailPhotos = aaActiveDetailPhotoMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveDetailPhoto aaActiveDetailPhoto:aaActiveDetailPhotos){
                idList.add(aaActiveDetailPhoto.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param aaActiveDetailPhoto 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertAaActiveDetailPhoto(AaActiveDetailPhoto aaActiveDetailPhoto) {
        if (Objects.isNull(aaActiveDetailPhoto)) {
            return -1;
        }
        try {
        
            //aaActiveDetailPhoto.setSpell(PinYin.getPinYin(aaActiveDetailPhoto.getName()));
            return aaActiveDetailPhotoMapper.insert(aaActiveDetailPhoto);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param aaActiveDetailPhoto 实体类
     * @return 结果
     */
    @Override
    public int updateAaActiveDetailPhotoById(AaActiveDetailPhoto aaActiveDetailPhoto) {
        if (Objects.isNull(aaActiveDetailPhoto)) {
            return -1;
        }
        try {
            //aaActiveDetailPhoto.setSpell(PinYin.getPinYin(aaActiveDetailPhoto.getName()));
            return aaActiveDetailPhotoMapper.updateById(aaActiveDetailPhoto);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteAaActiveDetailPhotoByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return aaActiveDetailPhotoMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<AaActiveDetailPhoto> getAaActiveDetailPhotoList() {
        try {
            LambdaQueryWrapper<AaActiveDetailPhoto> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetailPhoto());
            queryWrapper.isNotNull(AaActiveDetailPhoto::getId);
            return aaActiveDetailPhotoMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
